package com.primeton.wh.mcenter.impl.message.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.primeton.wh.mcenter.api.model.Message;
import com.primeton.wh.mcenter.api.response.Result;
import com.primeton.wh.mcenter.api.response.ResultGenerator;
import com.primeton.wh.mcenter.impl.message.service.IMessageService;
import com.primeton.wh.mcenter.impl.message.util.MessageConstants;
import feign.Param;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


/**
 * <p>
 * 消息表 前端控制器
 * </p>
 *
 * @author YunTao.Li
 * @since 2020-03-15
 */
@RestController
public class MessageController {

    @Autowired
    private IMessageService messageService;


    /**
     * 消息查询(分页)
     *
     * @param page      :
     * @param limit     :
     * @param msgTitle  :
     * @param startDate :
     * @param endDate   :
     * @return : com.primeton.wh.mcenter.impl.response.Result
     * @author : YunTao.Li
     * @date : 2020/3/17 2020/3/17
     */
    @ResponseBody
    @GetMapping("/message/list")
    @ApiOperation(value = "查询消息列表")
    public Result list(@ApiParam("当前页数") long page,
                       @ApiParam("每页条数") long limit,
                       @ApiParam("消息标题") String msgTitle,
                       @ApiParam("接收人") String msgTo,
                       @ApiParam("开始时间") String startDate,
                       @ApiParam("结束时间") String endDate,
                       @ApiParam("所属应用") String appCode) {

        // 构建查询条件
        Map<String, Object> condition = new HashMap<String, Object>();
        condition.put("page", page);
        condition.put("limit", limit);
        condition.put("msgTitle", msgTitle);
        condition.put("msgTo", msgTo);
        condition.put("startDate", startDate);
        condition.put("endDate", endDate);
        condition.put("appCode", appCode);

        IPage<Message> messageList = null;
        try {
            messageList = messageService.getMessageList(condition);
        } catch (Throwable e) {
            return ResultGenerator.genFailedResult(e.getMessage());
        }
        return Result.success(messageList);
    }


    /**
     * 重构   消息查询页面
     *
     * */
    @ResponseBody
    @GetMapping("/message/selectMessage")
    @ApiOperation("重构消息查询页面")
    public Result selectMessage(@ApiParam("当前页数")@Param("page") long page,
                                @ApiParam("每页条数")@Param("limit") long limit,
                                @ApiParam("接收人")  @Param("msgTo") String msgTo,
                                @ApiParam("开始时间")@Param("startDate") String startDate,
                                @ApiParam("结束时间")@Param("endDate") String endDate,
                                @ApiParam("所属应用")@Param("appCode") String appCode,
                                @ApiParam("消息标题")@Param("msgTitle") String msgTitle,
                                @ApiParam("应用名称")@Param("appName") String appName,
                                @ApiParam("消息状态")@Param("mcStatus") String mcStatus

    ){
        // 构建查询条件
        Map<String, Object> condition = new HashMap<String, Object>();
        condition.put("page", page);
        condition.put("limit", limit);
        condition.put("msgTitle", msgTitle);
        condition.put("msgTo", msgTo);
        condition.put("startDate", startDate);
        condition.put("endDate", endDate);
        condition.put("appCode", appCode);
        condition.put("appName", appName);
        condition.put("mcStatus", mcStatus);

        IPage<Message> messageList = null;
        try {
            messageList = messageService.selectMessage(condition);
        } catch (Throwable e) {
            return ResultGenerator.genFailedResult(e.getMessage());
        }
        return Result.success(messageList);



    }









    /**
     * 查询消息数量
     *
     * @param empCode :
     * @return : com.primeton.wh.mcenter.impl.response.Result
     * @author : YunTao.Li
     * @date : 2020/5/7 2020/5/7
     */
    @ResponseBody
    @GetMapping("/message/count/unread")
    @ApiOperation(value = "查询未读消息数量")
    public Result count(@RequestParam String empCode,
                        @RequestParam(required = false) String appCode) {

        // 构建查询条件
        Map<String, Object> condition = new HashMap<String, Object>();
        condition.put("empCode", empCode);
        condition.put("appCode", appCode);

        Integer messageCount = null;
        try {
            messageCount = messageService.getUnReadMessageCount(condition);
        } catch (Throwable e) {
            return ResultGenerator.genFailedResult(e.getMessage());
        }
        com.primeton.wh.mcenter.impl.message.param.Message message = new com.primeton.wh.mcenter.impl.message.param.Message();
        message.setCount(messageCount);

        return Result.success(message);
    }

    /**
     * 发送消息
     *
     * @param record :
     * @return : com.primeton.wh.mcenter.impl.response.Result
     * @author : YunTao.Li
     * @date : 2020/3/16 2020/3/16
     */
    @PostMapping("/message/send")
    @ResponseBody
    @ApiOperation(value = "消息发送")
    public Result sendMessage(@RequestBody Message record) {
        try {
            Map<String, String> result = messageService.sendMesage(record);
            if ("-1".equals(result.get("code"))) {
                String message = result.get("message");
                return ResultGenerator.genFailedResult(message);
            } else {
                return Result.success();
            }
        } catch (Throwable e) {
            return ResultGenerator.genFailedResult(e.getMessage());
        }
    }

    /**
     * 发送消息
     *
     * @return : com.primeton.wh.mcenter.impl.response.Result
     * @author : YunTao.Li
     * @date : 2020/3/16 2020/3/16
     */
    @PostMapping("/message/send/private")
    @ResponseBody
    @ApiOperation(value = "站内消息发送")
    public Result sendMessage(@RequestParam(required = true, name = "消息内容") String msgContent,
                              @RequestParam(required = false, name = "消息内容带格式") String msgContentHTML,
                              @RequestParam(required = true, name = "消息所属应用") String appCode,
                              @RequestParam(required = true, name = "消息来源系统") String fromSysCode,
                              @RequestParam(required = true, name = "消息来源机构") String fromOrgCode,
                              @RequestParam(required = true, name = "消息来源部门") String fromDeptCode,
                              @RequestParam(required = true, name = "消息来源人员编码") String fromEmpCode,
                              @RequestParam(required = true, name = "消息来源人员姓名") String fromEmpName,
                              @RequestParam(required = true, name = "消息送达系统") String toSysCode,
                              @RequestParam(required = true, name = "消息送达机构") String toOrgCode,
                              @RequestParam(required = true, name = "消息送达部门") String toDeptCode,
                              @RequestParam(required = true, name = "消息送达人员编码") String toEmpCode,
                              @RequestParam(required = true, name = "消息送达人员姓名") String toEmpName) {
        try {
            Message record = new Message();
            record.setMcType(MessageConstants.MESSAGE_TYPE_PRIMSG); //设置为站内消息
            record.setMcContent(msgContent);
            record.setMcContentHtml(msgContentHTML);
            record.setMcAppCode(appCode);
            record.setMcFromSysCode(fromSysCode);
            record.setMcFromOrgCode(fromOrgCode);
            record.setMcFromDeptCode(fromDeptCode);
            record.setMcFrom(fromEmpCode);
            record.setMcFromEmpName(fromEmpName);
            record.setMcToSysCode(toSysCode);
            record.setMcToOrgCode(toOrgCode);
            record.setMcToDeptCode(toDeptCode);
            record.setMcTo(toEmpCode);
            record.setMcToEmpName(toEmpName);

            Map<String, String> result = messageService.sendMesage(record);
            if ("-1".equals(result.get("code"))) {
                String message = result.get("message");
                return ResultGenerator.genFailedResult(message);
            } else {
                return Result.success();
            }
        } catch (Throwable e) {
            return ResultGenerator.genFailedResult(e.getMessage());
        }
    }

    /**
     * 新增消息
     *
     * @param record :
     * @return : com.primeton.wh.mcenter.impl.response.Result
     * @author : YunTao.Li
     * @date : 2020/3/16 2020/3/16
     */
    @PostMapping("/message/add")
    @ResponseBody
    @ApiOperation(value = "消息新增")
    public Result add(@RequestBody Message record) {
        try {
            Map<String, String> result = messageService.addMessage(record);
            if ("-1".equals(result.get("code"))) {
                String message = result.get("message");
                return ResultGenerator.genFailedResult(message);
            } else {
                return Result.success();
            }
        } catch (Throwable e) {
            return ResultGenerator.genFailedResult(e.getMessage());
        }
    }


    /**
     * 消息修改
     *
     * @param messageId :
     * @param record    :
     * @return : com.primeton.wh.mcenter.impl.response.Result
     * @author : YunTao.Li
     * @date : 2020/3/19 2020/3/19
     */
    @PutMapping("/message/{messageId}")
    @ResponseBody
    @ApiOperation(value = "消息修改")
    public Result update(@PathVariable String messageId,
                         @RequestBody Message record) {
        try {
            Map<String, String> result = messageService.editMessage(record);
            if ("-1".equals(result.get("code"))) {
                String message = result.get("message");
                return ResultGenerator.genFailedResult(message);
            } else {
                return Result.success();
            }
        } catch (Throwable e) {
            return ResultGenerator.genFailedResult(e.getMessage());
        }
    }
}

